$(function () {
    let isRelearr=[]
    let norelearr=[]
    let allarticle=[]
    init()
    // tab切换
    $('.tab-head>ul>li').click(function () {
        console.log(this)
        $(this).addClass('active').siblings('li').removeClass('active')
        var idx = $(this).index()
        $('.list>.main').eq(idx).addClass('selected').siblings('div').removeClass('selected')
    })
    $('.isRelli').click(function () {
        xianshi(10,1,isRelearr,1)
        var page = Math.ceil(isRelearr.length/10)
        $('#pagination').pagination({
            pageCount: page,
            jump: true,
            coping: true,
            homePage: '首页',
            endPage: '末页',
            prevContent: '上页',
            nextContent: '下页',
            current:1,  //当前第几页
            callback:function (api) {
                console.log(api.getCurrent())
                xianshi(10,api.getCurrent(),isRelearr,1)
            }
        })
    })
    $('.allarticle').click(function () {
        xianshi(10,1,allarticle,0)
        var page = Math.ceil(allarticle.length/10)
        $('#pagination').pagination({
            pageCount: page,
            jump: true,
            coping: true,
            homePage: '首页',
            endPage: '末页',
            prevContent: '上页',
            nextContent: '下页',
            current:1,  //当前第几页
            callback:function (api) {
                console.log(api.getCurrent())
                xianshi(10,api.getCurrent(),allarticle,0)
            }
        })
    })
    $('.noRelli').click(function () {
        xianshi(10,1,norelearr,2)
        var page = Math.ceil(norelearr.length/10)
        $('#pagination').pagination({
            pageCount: page,
            jump: true,
            coping: true,
            homePage: '首页',
            endPage: '末页',
            prevContent: '上页',
            nextContent: '下页',
            current:1,  //当前第几页
            callback:function (api) {
                console.log(api.getCurrent())
                xianshi(10,api.getCurrent(),norelearr,2)
            }
        })
    })
    // // 分页组件初始化
    // $('#pagination').pagination({
    //     pageCount: 7,
    //     jump: true,
    //     coping: true,
    //     homePage: '首页',
    //     endPage: '末页',
    //     prevContent: '上页',
    //     nextContent: '下页',
    //     current:1,  //当前第几页
    //     callback:function (api) {
    //         console.log(api.getCurrent())
    //         // xianshi(5,api.getCurrent())
    //     }
    // })
    // 详情
    $(document).on('click','.toarticle',function () {
        let div1 = $(this).closest("div")
        let divid = div1.find('.id').text()
        window.location.href='/unAuthorize/article?id='+divid
    })


    //编辑文章
    $(document).on('click','.editarticle',function () {
        console.log('dianj了')
        let div1 = $(this).closest("div")
        let divid = div1.find('.id').text()
        window.location.href='/editor/index?id='+divid
    })
    // 删除文章
    $(document).on('click','.delarticle',function () {
        let divli = $(this).parent().parent().parent()
        let div1 = $(this).closest("div")
        let divid = div1.find('.id').text()
        console.log(divid)
        $('.delebtn').click(function () {
            $.ajax({
                    url: '/article/delete?id='+divid,
                    type: 'Get',
                    dataType: "json",
                    // async: true,
                    processData: false,   // 不处理发送的数据
                    contentType: false,   // 不设置Content-Type请求头
                    success:
                        function (res) {
                            console.log(res)
                            if (res.code == 200) {
                                $.toast({
                                    title: '通知',
                                    subtitle: '1秒前',
                                    content: res.msg,
                                    type: 'success',
                                    delay: 3000,
                                    pause_on_hover: false
                                });
                                window.setInterval(function () {
                                    this.history.go(0)
                                },1000)
                            } else {
                                $.toast({
                                    title: '通知',
                                    subtitle: '1秒前',
                                    content: res.msg,
                                    type: 'error',
                                    delay: 3000,
                                    pause_on_hover: false
                                });
                            }
                        }
                }
            )

        })
    })




    function init() {
        $.ajax({
            url: '/article/list',
            type: 'Get',
            dataType: "json",
            // data: ,
            // async: true,
            processData: false,   // 不处理发送的数据
            contentType: false,   // 不设置Content-Type请求头
            success:
                function (res) {
                    if (res.code!==200){
                        $.toast({
                            title: '通知',
                            subtitle: '1秒前',
                            content: res.msg,
                            type: 'error',
                            delay: 3000,
                            pause_on_hover: false
                        });
                        return;
                    }
                    var data  = res.data
                    allarticle = res.data
                    console.log(data)
                    for(let i=0;i<data.length;i++){
                        if(data[i].isRelease==true){
                            isRelearr.push(data[i])
                        }
                        else {
                            norelearr.push(data[i])
                        }
                    }
                    // console.log("要发布的数组"+isRelearr)
                    // console.log(data.length)
                    xianshi(10,1,data,0)
                    var page = Math.ceil(data.length/10)
                    $('#pagination').pagination({
                        pageCount: page,
                        jump: true,
                        coping: true,
                        homePage: '首页',
                        endPage: '末页',
                        prevContent: '上页',
                        nextContent: '下页',
                        current:1,  //当前第几页
                        callback:function (api) {
                            console.log(api.getCurrent())
                            xianshi(10,api.getCurrent(),data,0)
                        }
                    })
                }
        })


    }

})

// 获取数据显示
function xianshi(num,page,data,listnum) {
    var page2 = (page-1)*10
    // 总页数

    if (listnum==0){
        $('.blogger-list').children().remove()
        for (let i = 0; i < num; i++) {
            if(data[i+page2]){
                var $str = $(
                    '<div class="blogger-list-li">'+
                    '<div class="blogger-list-title">'+
                    '<a href="/unAuthorize/article?id='+data[i+page2].id+'" title="点击进入查看文章"><span class="istrue">'+data[i+page2].isRelease+'</span><span>'+data[i+page2].title+'</span></a>'+
                    '</div>'+
                    '<div class="blogger-list-detail">' +
                    '<div class="blogger-list-detail-time" >' +
                    '<span>'+data[i+page2].createTime+'</span>' +
                    '</div>' +
                    '<div class="blogger-list-detail-btn" >' +
                    '<div class="id" style="display: none">'+data[i+page2].id+'</div>'+
                    '<button class="btn detailbtn toarticle fa fa-map  btn-primary">详情</button>' +
                    '<button class="btn detailbtn fa fa-edit editarticle btn-primary">编辑文章</button>' +
                    '<button class="btn detailbtn fa fa-times-circle delarticle btn-danger" data-target="#delmodal" data-toggle="modal">删除文章</button>' +
                    '</div>' +
                    '</div>'+
                    '</div>'
                )
                $('.blogger-list').append($str)
            }
            // console.log(data[i+page2].isComment)
        }
        $('.blogger-list .istrue').each(function () {
            if($(this).text()=='true'){
                $(this).text('（已发布） ')
            }
            else{
                $(this).text('（未发布） ')
            }

        })
    }
    if (listnum==1){
        $('.bloggercomment-list').children().remove()
        for (let i=0;i<num;i++){
            if(data[i+page2]){
                var $str = $(
                    '<div class="blogger-list-li">'+
                    '<div class="blogger-list-title">'+
                    '<a href="/unAuthorize/article?id='+data[i+page2].id+'" title="点击进入查看文章"><span  class="istrue">(已发布)</span><span>'+data[i+page2].title+'</span></a>'+
                    '</div>'+
                    '<div class="blogger-list-detail">' +
                    '<div class="blogger-list-detail-time" >' +
                    '<span>'+data[i+page2].createTime+'</span>' +
                    '</div>' +
                    '<div class="blogger-list-detail-btn" >' +
                    '<div class="id" style="display: none">'+data[i+page2].id+'</div>'+
                    '<button class="btn detailbtn toarticle fa fa-map  btn-primary">详情</button>' +
                    '<button class="btn detailbtn fa fa-edit editarticle btn-primary">编辑文章</button>' +
                    '<button class="btn detailbtn fa fa-times-circle delarticle btn-danger" data-target="#delmodal" data-toggle="modal">删除文章</button>' +
                    '</div>' +
                    '</div>'+
                    '</div>'
                )
                $('.bloggercomment-list').append($str)
            }

        }

    }
    if (listnum==2){
        $('.bloggernorel-list').children().remove()
        for (let i=0;i<num;i++){
            if(data[i+page2]){
                var $str = $(
                    '<div class="blogger-list-li">'+
                    '<div class="blogger-list-title">'+
                    '<a href="/unAuthorize/article?id='+data[i+page2].id+'" title="点击进入查看文章"><span  class="istrue">(未发布)</span><span>'+data[i+page2].title+'</span></a>'+
                    '</div>'+
                    '<div class="blogger-list-detail">' +
                    '<div class="blogger-list-detail-time" >' +
                    '<span>'+data[i+page2].createTime+'</span>' +
                    '</div>' +
                    '<div class="blogger-list-detail-btn" >' +
                    '<div class="id" style="display: none">'+data[i+page2].id+'</div>'+
                    '<button class="btn detailbtn toarticle fa fa-map  btn-primary">详情</button>' +
                    '<button class="btn detailbtn fa fa-edit editarticle btn-primary">编辑文章</button>' +
                    '<button class="btn detailbtn fa fa-times-circle delarticle btn-danger" data-target="#delmodal" data-toggle="modal">删除文章</button>' +
                    '</div>' +
                    '</div>'+
                    '</div>'
                )
                $('.bloggernorel-list').append($str)
            }

        }

    }


}


